package com.tool_stdy.IO.BIO;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * Created by yan_li on 2017/1/9.
 */
public class Server {

    private ServerSocket server;

    public Server(int port) throws IOException {
        this.server = new ServerSocket(port);
        System.out.println("服务端开始监听端口:" + port);
    }

    public void linstener() throws Exception {
        while (true) {
            Socket clent = server.accept();
            System.out.println("建立连接." + new Date());
            InputStream in = clent.getInputStream();
            byte [] buffer = new byte[1024];
            int len=in.read(buffer);
            if(len>0){
                String msg = new String(buffer,0,len);
                System.out.println(msg + "===" + new Date().toString());
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.linstener();
    }


}
